﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CXMLStudioIDE.Common.Entities;
using System.Data;
using CXMLStudioIDE.DataAccessLayer;
using System.Windows.Forms;

namespace CXMLStudioIDE.BusinessLayer
{
    public class SolutionManagerBL
    {
        private Solution _m_solution;
        private Project _m_project;
        private string m_CXMLUser;

        public SolutionManagerBL(string CXMLUser)
        {
            m_CXMLUser = CXMLUser;

        }
        public Solution m_solution
        {
            get { return _m_solution; }
            set { _m_solution = value; }
        }

        public Project m_project
        {
            get { return _m_project; }
            set { _m_project = value; }
        }
        //Solution related Methods
        public void CreateSolution(int i_SolutionID, string sZ_SolutionName)
        {
            try
            {
                m_solution = new Solution(i_SolutionID, sZ_SolutionName);
                if (i_SolutionID <= 0)
                {
                    SolutionManagerDAL dal = new SolutionManagerDAL();
                    DataSet ds = dal.SaveNewSolution(sZ_SolutionName, m_CXMLUser);
                    if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                    {
                        m_solution.ID = (int)ds.Tables[0].Rows[0].ItemArray[0];
                    }
                    else
                    {
                        throw new Exception("Could not save new Solution");
                    }
                }
            }
            catch (Exception e)
            {
                MessageBox.Show("Exception:" + e.Message, "CXMLStudioIDE", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void CreateProject()
        {
            m_project = new Project();
        }

        public void RemoveProjectFromSolution(Project p)
        {
            SolutionManagerDAL dal = new SolutionManagerDAL();
            dal.RemoveProjectFromSolution(m_solution.ID, p.ID);
            m_solution.Projects.Remove(p);
        }

        public void RemoveBusinessEntityFromProject(Project p, BusinessEntity b)
        {
            SolutionManagerDAL dal = new SolutionManagerDAL();
            dal.RemoveEntityVersionFromProject(p.ID, b.EntityTypeVersions.EntityTypeVersionsID);
            p.RemoveBusinessEntity(b);
        }

        public void OpenSolution(Solution s)
        {
            try
            {
                BusinessEntityCollection bec = new BusinessEntityCollection();
                SolutionManagerDAL sdal = new SolutionManagerDAL();
                CXMLStudioDAL dal = new CXMLStudioDAL();
                DataSet dsBusinessEntities = sdal.GetProjectsInSolution(s.ID);
                DataTable dtc = dsBusinessEntities.Tables[0];
                this.CreateSolution(s.ID, s.Name);
                foreach (DataRow r in dtc.Rows)
                {
                    Project p = new Project();
                    p.ID = (int)r["ProjectID"];
                    p.Name = r["ProjectName"].ToString();
                    p.Scope.ID = (int)r["ScopeID"];
                    p.Scope.Name = r["ScopeName"].ToString();
                    p.Context.ID = (int)r["ContextID"];
                    p.Context.Name = r["ContextName"].ToString();
                    m_solution.AddProjectToSolution(p);
                    DataSet dsEntitiesInProject = sdal.GetEntitiesForProject(p.ID);
                    foreach (DataRow e in dsEntitiesInProject.Tables[0].Rows)
                    {
                            BusinessEntity b = new BusinessEntity();
                            b.EntityType.EntityTypeID = (int)e["EntityTypeID"];
                            b.Name = e["EntityTypeName"].ToString();
                            b.ID = (int)e["EntityID"];
                            b.EntityVersion.FullVersion = e["Version"].ToString();
                            b.EntityType.EntityTypeContextID = (int)e["EntityTypeContextID"];
                            b.EntityType.EntityTypeContextName = e["EntityTypecontextName"].ToString();
                            b.EntityTypeVersions.EntityTypeVersionsID = (int)e["EntityTypeVersionID"];
                            b.EntityVersion.EntityVersionID = (int)e["EntityVersionID"];
                            b.EntityType.IsBusinessEntity = true;
                            b.ParentProject = p;
                            bec.Add(b);
                    }
                    p.Entities = bec;
                    bec = new BusinessEntityCollection();
                }
            }
            catch (Exception e)
            {
                MessageBox.Show("Exception:" + e.Message, "CXMLStudioIDE", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void AddEntityToProject(int i_ProjectID, BusinessEntity b)
        {
            SolutionManagerDAL dal = new SolutionManagerDAL();
            dal.AddExistingEntityVersionToProject(i_ProjectID, b.EntityTypeVersions.EntityTypeVersionsID);
            Project p = m_solution.Projects.GetProjectByID(i_ProjectID);
            if (p != null)
            {
                b.ParentProject = p;
                p.Entities.Add(b);
            }
        }
    }
}
